#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>indexed_multiset;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>indexed_set;
#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize("-O2")
#define all(a) (a).begin(), (a).end()
#define ll long long
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define bb begin()
#define ee end()
#define endll '\n'
#define pii pair<int,int>
#define pll pair<long long int,long long int>
#define vi vector<int>
#define vll vector<long long int>
#define vii vector<pii>
#define vpll vector<pll>
#define inf (1ll<<30ll)-1ll
#define llinf (1ll<<62ll)-1ll
#define mod 1000000007
#define n_size 200005
#define g_size 20000005
#define eps 1e-9
const double PI=acos(-1);
//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 way Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
//int dx[]={-1,-1,+0,+1,+1,+0};int dy[]={-1,+1,+2,+1,-1,-2}; //Hexagonal Direction
vi v,value;
inline void solve(){
ll n,m,t,res;
ll l,r,mid;
ll maxi=-inf,mini=inf,count=0;
ll i,j,k;
bool flag;
ll a1,ak;
cin>>m>>k>>a1>>ak;
ll need = m%k;
a1=a1-need;
m=m-need;
if(a1<0){
count-=a1;
a1=0;
}
m=m/k;
m=m-min(ak,m);
m=m-min(a1/k,m);
count+=m;
cout<<count<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);
//cout<<fixed<<setprecision(11);
int T=1;
cin>>T;
while(T--) solve();
}
1088B - Ehab and subtraction | 1270B - Interesting Subarray |
478C - Table Decorations | 1304C - Air Conditioner |
1311C - Perform the Combo | 1519C - Berland Regional |
361A - Levko and Table | 5E - Bindian Signalizing |
687A - NP-Hard Problem | 1542C - Strange Function |
961E - Tufurama | 129D - String |
888A - Local Extrema | 722B - Verse Pattern |
278A - Circle Line | 940A - Points on the line |
1742C - Stripes | 1742F - Smaller |
1742B - Increasing | 1742A - Sum |
1742D - Coprime | 390A - Inna and Alarm Clock |
1398B - Substring Removal Game | 1742G - Orray |
416B - Art Union | 962A - Equator |
803B - Distances to Zero | 291A - Spyke Talks |
1742E - Scuza | 1506D - Epic Transformation |